package com.TPNexus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import com.TPNexus.dao.CatalogItemDAO;
import com.TPNexus.dao.ProductDAO;
import com.TPNexus.dao.PurchaseOrderDAO;
import com.TPNexus.dao.ShoppingCartDAO;
import com.TPNexus.model.CatalogItem;
import com.TPNexus.model.PurchaseOrder;
import com.TPNexus.model.PurchaseOrderLineItem;
import com.TPNexus.model.ShoppingCart;
import com.TPNexus.model.ShoppingCartItem;

@Named
@SessionScoped
public class CatalogPagesController implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Inject
	private CatalogItemDAO catalogItemDAO;
	// @Inject
	// private Conversation conversation;
	@Inject
	ProductDAO productDAO;
	@Inject
	ShoppingCartDAO shoppingcartDAO;
	@Inject
	PurchaseOrderDAO poDAO;

	// member variable / getter&setter
	// member-add new
	private CatalogItem ncatalogitem;
	// member-detail and delete
	private CatalogItem catalogitem;
	// member-update
	private CatalogItem ucatalogitem;
	// member-shopping cart item
	private ShoppingCartItem shoppingcartitem;
	// member-shopping
	private ShoppingCart shoppingcart;
	private Date deliverydate;

	// Constructor
	public CatalogPagesController() {
		super();
	}

	// necessary ?
	@PostConstruct
	public void init() {
		this.shoppingcartitem = new ShoppingCartItem();
		this.shoppingcart = shoppingcartDAO.getShoppingCartbyUser((long) 1);

		if (this.getShoppingcart() == null) {
			shoppingcart = new ShoppingCart();
			shoppingcart.setUserId(1);
			shoppingcartDAO.creatNewShoppingCart(shoppingcart);

		}

		// catalog item = new CatalogItem();
		//
		// catalogitem.setStatus(true);
		System.out.println("Line 32");
	}

	public CatalogItem getnCatalogitem() {
		// if (conversation.isTransient())
		// conversation.begin("new");
		// conversation.getTimeout();
		return ncatalogitem;
	}

	public void setnCatalogitem(CatalogItem ncatalogitem) {
		this.ncatalogitem = ncatalogitem;
	}

	public CatalogItem getCatalogitem() {
		return catalogitem;
	}

	public void setCatalogitem(CatalogItem catalogitem) {
		this.catalogitem = catalogitem;
	}

	public CatalogItem getUcatalogitem() {
		// if (conversation.isTransient())
		// conversation.begin("update");

		return ucatalogitem;
	}

	public void setUcatalogitem(CatalogItem ucatalogitem) {
		this.ucatalogitem = ucatalogitem;
	}

	public ShoppingCartItem getShoppingcartitem() {
		return shoppingcartitem;
	}

	public void setShoppingcartitem(ShoppingCartItem shoppingcartitem) {
		this.shoppingcartitem = shoppingcartitem;

	}

	public ShoppingCart getShoppingcart() {
		return shoppingcart;
	}

	public void setShoppingcart(ShoppingCart shoppingcart) {
		this.shoppingcart = shoppingcart;
	}

	// Load Data
	public ArrayList<CatalogItem> getCatalogItemList() {
		ArrayList<CatalogItem> list = new ArrayList<CatalogItem>(
				catalogItemDAO.getCatalogItemList());
		return list;
	}

	// Add a new catalog item
	public String toAddNewCatalogItem() {
		ncatalogitem = new CatalogItem();
		ncatalogitem.setStatus(true);
		return "AddNewCatalogItem";
	}

	public String addToConfirmation() {
		// HttpServletRequest request = (HttpServletRequest) FacesContext
		// .getCurrentInstance().getExternalContext().getRequest();
		// Integer id = Integer.parseInt(request.getParameter("id"));
		// ncatalogitem.setProduct(productDAO.getProductDetail(id));
		return "NewCatalogItemConfirmation";
	}

	public String addNewCatalogItem() {
		// catalogitem.getProduct();
		catalogItemDAO.addNewCatalogItem(ncatalogitem);
		// conversation.end();
		// conversation.setTimeout(1000l);
		return "CatalogItemListSellerView";
	}

	// catalog item detail
	public String getCatalogItemDetailSellerView() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		catalogitem = catalogItemDAO.getCatalogItemDetail(id);
		return "CatalogItemDetailSellerView";
	}

	public String getCatalogItemDetailBuyerView() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		catalogitem = catalogItemDAO.getCatalogItemDetail(id);
		return "CatalogItemDetailBuyerView";
	}

	// update catalog item
	public String toUpdateCatalogItem() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		ucatalogitem = catalogItemDAO.getCatalogItemDetail(id);
		return "UpdateCatalogItem";
	}

	public String updateToConfirmation() {
		return "UpdateCatalogItemConfirmation";
	}

	public String updateCatalogItem() {
		catalogItemDAO.updateCatalogItem(ucatalogitem);
		// String supdate = conversation.getId();
		// System.out.println("supdate=" + supdate);
		// if (!conversation.isTransient())
		// conversation.end();
		// conversation.setTimeout(1000l);
		return "CatalogItemListSellerView";
	}

	// delete
	public String deleteCatalogItem() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		catalogitem = catalogItemDAO.getCatalogItemDetail(id);
		catalogItemDAO.deleteCatalogItem(catalogitem);
		return "CatalogItemListSellerView";
	}

	// add to shopping cart
	public String addToShoppingCart() {
		shoppingcartitem.setCatalogItem(catalogitem);
		shoppingcartDAO.addToShoppingCart(shoppingcartitem, shoppingcart);
		shoppingcartitem = new ShoppingCartItem();

		// shoppingcart.getShoppingCartItems().add(shoppingcartitem);
		return "ViewShoppingCart";
	}

	public void valueChanged() {
		System.out.println("---------------");
	}

	// shopping cart page update quantity
	public void qtyChanged(ValueChangeEvent e) {

		shoppingcartDAO.updateShoppingCart(this.shoppingcart);
	}

	// create purchase order
	public String createPurchaseOrder() {
		this.poDAO.createPurchaseOrder(shoppingcart, this.deliverydate);
		shoppingcartDAO.deleteShoppingCart(shoppingcart);
		init();
		return "ThankYou";
	}

	public Date getDeliverydate() {
		return deliverydate;
	}

	public void setDeliverydate(Date deliverydate) {
		this.deliverydate = deliverydate;
	}

	// assign new value to localeCode
	// public String getCatalogItemDetail(long catalogitemid){
	// HttpServletRequest request = (HttpServletRequest) FacesContext
	// .getCurrentInstance().getExternalContext().getRequest();
	// Integer id = Integer.parseInt(request.getParameter("id"));
	// catalogitem=catalogItemDAO.getCatalogItemDetail(id);
	// return "CatalogItemDetail";
	// }

}
